<?php

global 	$tab_feature_b1,
        $tab_feature_b2,
        $tab_count_b1,
        $tab_count_b2,
        $screen_width;
        
global  $tab_b1_titles,
				$tab_b2_titles;


////////////////////////////////////////////////////////////////////////////////
// register/unregister bottom bars
////////////////////////////////////////////////////////////////////////////////

function register_bottom_bar_b1()
{
	if ( function_exists('register_sidebar') )
		register_sidebar(array(
			'name' => 'Bottom_Menu_1',
			'id' => 'Bottom_Menu_1',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
			));
}

function register_bottom_bar_b2()
{
	if ( function_exists('register_sidebar') )
		register_sidebar(array(
			'name' => 'Bottom_Menu_2',
			'id' => 'Bottom_Menu_2',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
			));
}

function unregister_bottom_bar_b1()
{
	if ( function_exists('unregister_sidebar') )
	  unregister_sidebar('Bottom_Menu_1');
}

function unregister_bottom_bar_b2()
{
	if ( function_exists('unregister_sidebar') )
	  unregister_sidebar('Bottom_Menu_2');
}



////////////////////////////////////////////////////////////////////////////////
// register/unregister bottom tabs
////////////////////////////////////////////////////////////////////////////////
function register_b1_tabs()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2;

	if ( function_exists('register_sidebar') )
	{
		for ( $i = 1; $i <= $tab_count_b1; $i++ )
		{
		  $name = 'Tab-Panel-1_Tab-' . $i;
		  
			register_sidebar(array(
				'name' => $name,
				'id' => $name,
				'before_title' => '<h2>',
				'after_title' => '</h2>',
				));
		}
	}
}

function register_b2_tabs()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2;

	if ( function_exists('register_sidebar') )
	{
		for ( $i = 1; $i <= $tab_count_b2; $i++ )
		{
		  $name = 'Tab-Panel-2_Tab-' . $i;

			register_sidebar(array(
				'name' => $name,
				'id' => $name,
				'before_title' => '<h2>',
				'after_title' => '</h2>',
				));
		}
	}
}


////////////////////////////////////////////////////////////////////////////////
//	add tab titles options
////////////////////////////////////////////////////////////////////////////////
function	add_tab_b1_titles()
{
	global  $tab_b1_titles,
	        $tab_count_b1;

	$def_titles = '';
	for ( $i = 1; $i <= $tab_count_b1; $i++ )
	{
	  $def_titles[ $i ] = 'Tab' . $i;
	}

	add_option( 'gn_tab_b1_titles', $def_titles );
}

function	add_tab_b2_titles()
{
	global  $tab_b2_titles,
	        $tab_count_b2;

	$def_titles = '';
	for ( $i = 1; $i <= $tab_count_b2; $i++ )
	{
	  $def_titles[ $i ] = 'Tab' . $i;
	}

	add_option( 'gn_tab_b2_titles', $def_titles );
}


////////////////////////////////////////////////////////////////////////////////
//	get titles
////////////////////////////////////////////////////////////////////////////////
function  get_tab_b1_titles()
{
	global  $tab_b1_titles;
	
	$tab_b1_titles 	= get_option('gn_tab_b1_titles');
}

function  get_tab_b2_titles()
{
	global  $tab_b2_titles;

	$tab_b2_titles 	= get_option('gn_tab_b2_titles');
}


////////////////////////////////////////////////////////////////////////////////
// load theme wp-admin css
////////////////////////////////////////////////////////////////////////////////

function gn_load_css()
{
	$theme_option_css_url = get_template_directory_uri();
	
	echo 	"\n\n";
	echo 	'<!-- German NewsPaper - Theme Option CSS -->' . "\n";
	echo 	'<!--[if gte IE 6]>' . "\n" . '<link rel="stylesheet" href="' . $theme_option_css_url . '/css/theme-option.css" type="text/css" />' . "\n" . '<![endif]-->' . "\n";
	echo 	'<link rel="stylesheet" type="text/css" href="' . $theme_option_css_url .
				'/css/theme-option.css" />';
	echo 	"\n\n";
}


////////////////////////////////////////////////////////////////////////////////
// get posts with most comments
////////////////////////////////////////////////////////////////////////////////

function get_mostcommented($limit = 5)
{
	global $wpdb, $post;

	$mostcommenteds = $wpdb->get_results("SELECT  $wpdb->posts.ID, post_title, post_name, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->posts LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' GROUP BY $wpdb->comments.comment_post_ID ORDER  BY comment_total DESC LIMIT $limit");

	foreach ($mostcommenteds as $post)
	{
		$post_title = htmlspecialchars(stripslashes($post->post_title));
		$comment_total = (int) $post->comment_total;
		echo "<li><a href=\"".get_permalink()."\">$post_title&nbsp;<strong>($comment_total)</strong></a></li>";
	}
}



////////////////////////////////////////////////////////////////////////////////
//	check s creen width
//
////////////////////////////////////////////////////////////////////////////////
function  check_screen_width()
{
	global  $screen_width;
	
	$screen_width = get_option('gn_screen_width');
}

////////////////////////////////////////////////////////////////////////////////
//	check tab feature
//	if necessary register tabs
////////////////////////////////////////////////////////////////////////////////

function  check_tab_feature()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2;

	$tab_feature_b1 = get_option('gn_tab_feature_b1');
	$tab_feature_b2 = get_option('gn_tab_feature_b2');
	$tab_count_b1 	= get_option('gn_tab_count_b1');
	$tab_count_b2 	= get_option('gn_tab_count_b2');

	if ( $tab_feature_b1 == 'off' )
	{
  	register_bottom_bar_b1();
	}
	else
	{
	  register_b1_tabs();
	}
	
	if ( $tab_feature_b2 == 'off' )
	{
		register_bottom_bar_b2();
	}
	else
	{
	  register_b2_tabs();
	}

  add_tab_b1_titles();
  add_tab_b2_titles();
}



////////////////////////////////////////////////////////////////////////////////
//	update theme options
////////////////////////////////////////////////////////////////////////////////

function  update_gn_options()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2,
	        $screen_width;

  $new_gn_tab_b1_titles = array();
  $new_gn_tab_b2_titles = array();

	//  check if remove all widgets is set
	if ( isset( $_POST['new_gn_rem_widgets'] ) )
	{
  	gn_remove_all_widgets();
	}
	
	//  check if screen width checkbox is set --> 800px
	if (!isset( $_POST['new_gn_screen_width'] ))
	{
	  $_POST['new_gn_screen_width'] = 'off';
	  $screen_width = 'off';
	}
	else
	{
	  $_POST['new_gn_screen_width'] = 'on';
	  $screen_width = 'on';
	}

	
	//  check if tab feature has changed and
	//  register or unregister sidebars accordingly
	if (!isset( $_POST['new_gn_tab_feature_b1'] ))
	{
	  $_POST['new_gn_tab_feature_b1'] = 'off';
	  
	  if ( get_option('gn_tab_feature_b1') == 'on' )
	  {
	    //  unregister tabs
	    if ( function_exists('unregister_sidebar') )
	    {
	      $tab_b1_old_count = get_option('gn_tab_count_b1');

	      for ( $i = 1; $i <= $tab_b1_old_count; $i++ )
	      {
				  $name = 'Tab-Panel-1_Tab-' . $i;
	        unregister_sidebar($name);
	      }
	    }
	    //  and register bottom_bar_b1
	    register_bottom_bar_b1();
	  }
	  else
	  {
	    //  tab feature was off ==> nothing to do
	  }
    //  and set varaible to off
    $tab_feature_b1 = 'off';
	}
	else
	{
	  $_POST['new_gn_tab_feature_b1'] = 'on';
	  
	  if ( get_option('gn_tab_feature_b1') == 'off')
	  {
	    //  first unregister bottom_menu_b1
	    unregister_bottom_bar_b1();
	    
	    //  and then register tabs
			if ( function_exists('register_sidebar') )
			{
				$tab_count_b1 = $_POST['new_gn_tab_count_b1'];
				for ( $i=1; $i <= $tab_count_b1; $i++ )
				{
				  $name = 'Tab-Panel-1_Tab-' . $i;
					register_sidebar(array(
						'name' => $name,
						'id' => $name,
						'before_title' => '<h2>',
						'after_title' => '</h2>',
						));
				}
			}
	  }
	  else
	  {
	    //  check if count has changed
			$tab_count_b1 = $_POST['new_gn_tab_count_b1'];
      $tab_b1_old_count = get_option('gn_tab_count_b1');

			if ( $tab_b1_old_count != $tab_count_b1 )
			{
			  if ( $tab_b1_old_count > $tab_count_b1 )
			  {
			    //  unregister sidebars
			    for ( $i = $tab_count_b1 + 1; $i <= $tab_b1_old_count; $i++)
			    {
					  $name = 'Tab-Panel-1_Tab-' . $i;
       	    if ( function_exists('unregister_sidebar') )
       	      unregister_sidebar( $name );
			    }
			  }
			  else
			  {
			    //  register sidebars
			    for ( $i = $tab_b1_old_count + 1; $i <= $tab_count_b1; $i++)
			    {
					  $name = 'Tab-Panel-1_Tab-' . $i;
			      if ( function_exists('register_sidebars') )
			      {
							register_sidebar(array(
								'name' => $name,
								'id' => $name,
								'before_title' => '<h2>',
								'after_title' => '</h2>',
								));
    	      }
			    }
			  }
			}
	  }
		//  and set variable to on
		$tab_feature_b1 = 'on';
	}

	if (!isset( $_POST['new_gn_tab_feature_b2'] ))
	{
	  $_POST['new_gn_tab_feature_b2'] = 'off';

	  if ( get_option('gn_tab_feature_b2') == 'on' )
	  {
	    //  unregister tabs
      $tab_b2_old_count = get_option('gn_tab_count_b2');

      for ( $i = 1; $i <= $tab_b2_old_count; $i++ )
      {
			  $name = 'Tab-Panel-2_Tab-' . $i;
        unregister_sidebar($name);
      }
      
      //  register Bottom Bar 2
      register_bottom_bar_b2();
	  }
	  else
	  {
	    //  tab feature was off ==> nothing to do
	  }
    //  and set varaible to off
    $tab_feature_b2 = 'off';
	}
	else
	{
	  $_POST['new_gn_tab_feature_b2'] = 'on';

	  if ( get_option('gn_tab_feature_b2') == 'off')
	  {
	    //  first unregister bottom_menu_b2
	    unregister_bottom_bar_b2();

	    //  and then register tabs
			if ( function_exists('register_sidebar') )
			{
				$tab_count_b2 = $_POST['new_gn_tab_count_b2'];
				for ( $i=1; $i <= $tab_count_b2; $i++ )
				{
				  $name = 'Tab-Panel-2_Tab-' . $i;
					register_sidebar(array(
						'name' => $name,
						'id' => $name,
						'before_title' => '<h2>',
						'after_title' => '</h2>',
						));
				}
			}
	  }
	  else
	  {
	    //  check if count has changed
			$tab_count_b2 = $_POST['new_gn_tab_count_b2'];
      $tab_b2_old_count = get_option('gn_tab_count_b2');

			if ( $tab_b2_old_count != $tab_count_b2 )
			{
			  if ( $tab_b2_old_count > $tab_count_b2 )
			  {
			    //  unregister sidebars
			    for ( $i = $tab_count_b2 + 1; $i <= $tab_b2_old_count; $i++)
			    {
					  $name = 'Tab-Panel-2_Tab-' . $i;
       	    if ( function_exists('unregister_sidebar') )
       	      unregister_sidebar( $name );
			    }
			  }
			  else
			  {
			    //  register sidebars
			    for ( $i = $tab_b2_old_count + 1; $i <= $tab_count_b2; $i++)
			    {
					  $name = 'Tab-Panel-2_Tab-' . $i;
			      if ( function_exists('register_sidebars') )
			      {
							register_sidebar(array(
								'name' => $name,
								'id' => $name,
								'before_title' => '<h2>',
								'after_title' => '</h2>',
								));
    	      }
			    }
			  }
			}
	  }
		//  and set variable to on
		$tab_feature_b2 = 'on';
	}

	//  build
	//  gn_tab_b1_titles      and
	//  gn_tab_b2_titles
	for ( $i = 1; $i <= $tab_count_b1; $i++)
	{
	  $field_name = 'new_gn_b1_tab_' . $i . '_title';
		$new_gn_tab_b1_titles[ $i ] = $_POST[$field_name];
	}
	for ( $i = 1; $i <= $tab_count_b2; $i++)
	{
	  $field_name = 'new_gn_b2_tab_' . $i . '_title';
		$new_gn_tab_b2_titles[ $i ] = $_POST[$field_name];
	}

	update_option( 'gn_left_img', $_POST['new_gn_left_img'] );
	update_option( 'gn_right_img', $_POST['new_gn_right_img'] );
	update_option( 'gn_page_exclude', $_POST['new_gn_page_exclude'] );
	update_option( 'gn_body_color', $_POST['new_gn_body_color'] );
	update_option( 'gn_body_background', $_POST['new_gn_body_background'] );
	update_option( 'gn_wrapper_background', $_POST['new_gn_wrapper_background'] );
	update_option( 'gn_screen_width', $_POST['new_gn_screen_width'] );
	update_option( 'gn_tab_feature_b1', $_POST['new_gn_tab_feature_b1'] );
	update_option( 'gn_tab_feature_b2', $_POST['new_gn_tab_feature_b2'] );
	update_option( 'gn_tab_count_b1', $_POST['new_gn_tab_count_b1'] );
	update_option( 'gn_tab_count_b2', $_POST['new_gn_tab_count_b2'] );
	update_option( 'gn_tab_b1_titles', $new_gn_tab_b1_titles );
	update_option( 'gn_tab_b2_titles', $new_gn_tab_b2_titles );

	_e("	<div class=\"updated\">
					<p><strong>Options updated!</strong></p>
				</div>\n");
				
	if ( isset( $_POST['new_gn_rem_widgets'] ) )
	{
		_e("	<div class=\"updated\">
						<p><strong>All registered widgets deleted !!!</strong></p>
					</div>\n");
	}
}




?>